<template>
  <div class="dashboard-container">
    <div class="app-container">
      <PageTools :show-before="false">
        <template #after>
          <el-button size="small" type="primary" @click="addPermissionFn(1,'0')">添加权限</el-button>
        </template>
      </PageTools>
      <el-table border :data="permissionList" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button v-if="row.type!==2" type="text" @click="addPermissionFn(2,row.id)">添加</el-button>
            <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--  -->
      <el-dialog title="新增权限" :visible="showDialog" @close="closeFn">
        <el-form ref="form" label-width="120px" :rules="rules" :model="formData">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="开启" prop="enVisable">
            <el-switch v-model="formData.enVisable" active-value="1" inactive-value="0" />
          </el-form-item>
        </el-form>
        <span slot="footer">
          <el-row type="flex" justify="center">
            <el-button size="small" type="primary" @click="confirm">确定</el-button>
            <el-button size="small" @click="closeFn">取消</el-button>
          </el-row>
        </span>
      </el-dialog>
    </div>
  </div>

</template>

<script>
import { getPermissionList, addPermission, getPermissioDetail, updatePermissio, DeletePermissio } from '../../api/permission'
import { listtotree } from '../../utils/index'
export default {
  data() {
    return {
      permissionList: [],
      showDialog: false,
      formData: {
        name: '', // 权限名称
        enVisable: '', // 开启
        code: '', // 权限设置
        description: '',
        type: 1,
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空！', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空！', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '权限描述不能为空！', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getPermission()
  },
  methods: {
    // 删除
    async  delPermission(id) {
      this.$confirm('是否删除', '提示', {
        type: 'warning'
      }).then(async() => {
        await DeletePermissio(id)
        this.getPermission()
      }).catch(err => {
        console.log(err, '点击了取消')
      })
    },
    // 回显的方法
    async  editPermission(id) {
      console.log('id: ', id)
      this.showDialog = true
      // const res = await getPermissioDetail(id)
      // console.log('res: ', res)
      this.formData = await getPermissioDetail(id)
      //
    },
    // 封装一个渲染函数的方法
    async  getPermission() {
      this.permissionList = await getPermissionList()
      this.permissionList = listtotree(this.permissionList, '0')
    },
    addPermissionFn(type, pid) {
      this.showDialog = true
      // 点击“添加权限”按钮，是把type和pid传递过来，并保存起来
      this.formData.type = type
      this.formData.pid = pid
    },
    closeFn() {
      this.formData = {
        name: '', // 权限名称
        enVisable: '', // 开启
        code: '', // 权限设置
        description: '',
        type: 1,
        pid: ''
      }
      this.showDialog = false
      // 清空页面
      this.$refs.form.resetFields()
    },
    async  confirm() {
      await this.$refs.form.validate()
      console.log('校验通过')
      // 通过判断有没有ID来执行不同的调用接口，如果有id就是编辑，没用就是添加
      if (this.formData.id) {
        await updatePermissio(this.formData)
      } else {
        await addPermission(this.formData)
      }
      // 调用接口
      this.getPermission()
      this.closeFn()
    }
  }
}
</script>

<style>
.el-table{
  margin-top: 20px;
}
</style>
